/**
 * Created by zengy on 2017/8/1.
 */
angular.module('mainApp.practiceManage', ['ui.router'])
    .config(function($stateProvider) {
        $stateProvider
            .state({//模板列表页面
                name : 'practiceManage/practiceList',
                url:'/practiceManage/practiceList',
                templateUrl:'views/practiceManage/practice_list.html?v='+new Date().getMinutes(),
                controller:'practiceCtrl'
            })
    })
    .controller('practiceCtrl',function ($scope) {
        $scope.$emit('arg',11);
        $scope.basePictureUrl=basePictureUrl;
        $scope.practiceModel={
            pageNo:1,
            pageSize:10
        }
        $scope.addressModel={};
        $scope.pagination = function (currentPage, pageCount) {
            $('.page-function #pagination').remove();
            $('.page-function').html('<ul id="pagination" class="pagination"></ul>');
            $('.pagination').twbsPagination({
                totalPages: pageCount,
                startPage: currentPage,
                visiblePages: 5,
                first: "首页",
                prev: "上一页",
                next: "下一页",
                last: "尾页",
                href: 'javascript:;',
                //hideOnlyOnePage : true,
                onPageClick: function (event, page) {
                    $scope.getPractice(page);
                }
            });
        };
        $scope.getPractice=function (page) {
            if(page){
                $scope.practiceModel.pageNo=page;
            }
            new HttpRequest().getPractice($scope.practiceModel,function (res) {
                if(res.code=='000'){
                    $scope.practiceList = res.data.list;
                    $scope.$apply();
                    if ($scope.practiceList.length > 0) {
                        $scope.pagination($scope.practiceModel.pageNo, res.data.pageCount);
                    }
                } else {
                    layer.msg(res.msg)
                }

            })
        }
        $scope.getPractice($scope.practiceModel.pageNo);

        var map = new BMap.Map("allmap");
        //map start-------------------
        var point = new BMap.Point(103.856944, 30.781381);  // 创建点坐标
        map.centerAndZoom(point,11);
        map.enableScrollWheelZoom(true);
        function showInfo(e){
            map.clearOverlays();
            var marker = new BMap.Marker(new BMap.Point(e.point.lng, e.point.lat)); // 创
            map.addOverlay(marker);    //增加点
            $('input[name="coordinate"]').val( e.point.lng+','+ e.point.lat)
        }
        map.addEventListener("click", showInfo);
        map.enableScrollWheelZoom(true);
        $('input[name="addressName"]').blur(function () {
            map.clearOverlays();
            var keyword =$(this).val();
            var searchObj = new BMap.LocalSearch(map, {
                renderOptions:{
                    map: map
                },
                pageCapacity : 1
            });
            searchObj.search(keyword);
        })
        //map end--------------------
        //上传封面功能
        $scope.uploadImg = function (current, type) {
            if(current.files.length>0){
                $('.imgCover').attr('src','./img/img-cover.png');
                new HttpRequest().uploadFile(current,type,function (res) {
                    if(res.code == '000'){
                        var resData = JSON.parse(res.data).url;
                        console.log(resData);
                        $scope.addressModel.picUrl = resData;
                        $('.imgCover').attr('src',basePictureUrl+resData);
                    }else{
                        layer.msg('上传封面失败'+'('+res.msg+')');
                    }
                })
            }
        };
        $scope.commitAddress=function () {
            $scope.addressModel={
                name:$('input[name="addressName"]').val(),//地址名称
                summary:editor.html(),//简介
                picUrl:$scope.addressModel.picUrl,//封面
                longitude:$('input[name="coordinate"]').val().split(',')[0],//经度
                latitude:$('input[name="coordinate"]').val().split(',')[1],//维度
                enable:'1'//0禁用 1启用
            }
            for(var attr in $scope.addressModel){
                if(!$scope.addressModel[attr]){
                    layer.msg('请完善信息');
                    return false;
                }
            }
            $scope.addressModel.rowId='';
            new HttpRequest().saveAddress($scope.addressModel,function (res) {
                if(res.code=='000'){
                    location.reload();
                }else{
                    layer.msg(res.msg);
                }
            })
        }
        //禁用，启用
        $scope.changeEnable=function (event) {
            var _this=$(event.target);
            var wecaId=_this.attr('data-id');
            var enable=_this.attr('data-status');
            enable=(enable=='0') ? '1':0;
            new HttpRequest().modifyEnable({wecaId:wecaId,enable:enable},function (res) {
                if(res.code=='000'){
                    location.reload()
                }else{
                    layer.msg(res.msg);
                }
            })
        }

    })
